#ifndef     __MAKE_ICE_TASK_H
#define     __MAKE_ICE_TASK_H

#include "../../rtos/thread.h"
#include "../../utils/object.h"
#include "../../rtos/sync_value.h"

namespace task {

class MakeIceTask : public rtos::Thread<MakeIceTask, 1024, osPriorityHigh1> {

public:
    enum class State : uint8_t {
        IDLE = 0,
        STIR_BEGIN = 1,
        WORK = 2,
        STIR_END = 3,
        STOP = 4,
        TIMEOUT = 5,
        INVALID = 6,
        FAIL = 7,
    };

    static constexpr uint32_t MAKE_ICE_TIMEOUT = 150 * 60 * 1000;

    static constexpr uint32_t RUN_STIR_COMPR_DELAY = 10 * 1000;
    static constexpr uint32_t STOP_STIR_COMPR_DELAY = 20 * 1000;
    static constexpr uint32_t RESET_STIR_COMPR_DELAY = 20 * 1000;

    inline void auto_make() { set(State::IDLE); }
    inline void stop() { set(State::STOP); }

    void run();

    inline uint8_t state() { return static_cast<uint8_t>(m_state); }

private:
    void set(State state) { m_sync.set(state); }
    bool wait(uint32_t timeout = osWaitForever) { return m_sync.get(m_state, timeout); }

    bool can_start();

    void do_idle();
    void do_stir_begin();
    void do_work();
    void do_stir_end();
    void do_stop();

    uint32_t m_stir_compr_delay = RESET_STIR_COMPR_DELAY;
    State m_state = State::IDLE;
    rtos::SyncValue<State> m_sync;
};

inline utils::Object<MakeIceTask> makeIceTask;

}

#endif
